package com.xinyuan.digit.next.subscriber.db.pojo;

import java.util.ArrayList;
import java.util.List;

/**
 * SQL排序封装
 *
 * @author LiuChenghui 2025/10/23 15:21:00
 * @version 1.0
 */
public class OrderByBuilder {
    private final List<OrderItem> orderItems = new ArrayList<>();

    /**
     * 添加升序排序字段
     */
    public <T> OrderByBuilder asc(SFunction<T, ?> column) {
        orderItems.add(new OrderItem(column, "ASC"));
        return this;
    }

    /**
     * 添加降序排序字段
     */
    public <T> OrderByBuilder desc(SFunction<T, ?> column) {
        orderItems.add(new OrderItem(column, "DESC"));
        return this;
    }

    /**
     * 批量添加排序字段（全部升序）
     */
    @SafeVarargs
    public final <T> OrderByBuilder ascAll(SFunction<T, ?>... columns) {
        for (SFunction<T, ?> column : columns) {
            orderItems.add(new OrderItem(column, "ASC"));
        }
        return this;
    }

    /**
     * 批量添加排序字段（全部降序）
     */
    @SafeVarargs
    public final <T> OrderByBuilder descAll(SFunction<T, ?>... columns) {
        for (SFunction<T, ?> column : columns) {
            orderItems.add(new OrderItem(column, "DESC"));
        }
        return this;
    }

    /**
     * 获取排序项列表
     */
    public List<OrderItem> getOrderItems() {
        return new ArrayList<>(orderItems);
    }

    /**
     * 判断是否有排序条件
     */
    public boolean hasOrder() {
        return !orderItems.isEmpty();
    }

    /**
     * 清除所有排序条件
     */
    public void clear() {
        orderItems.clear();
    }
}
